$(function() {
	
	
	//区域联动
	$.ajax({
		url : ctx + '/manag/area/findArea', //省
		data : {type:2},
		dataType : 'json',
		type : 'post',
		success : function(json){
			
			var data = json.data;
			
			if(json.status == "success"){
				
				var selected = $('#s_province').data('selected');
				
				$('#s_province').html('<option value="0">请选择</option>');
				
				for(var i=0; i<data.length; i++){
					
					if(selected != null &&　selected == data[i].id){
						$('#s_province').append('<option selected="selected" value="'+data[i].id+'">'+data[i].name+'</option>');
						$('#s_province').change();
					}else{
						$('#s_province').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
					}
				}
			}
		},
		error : function(){
			toastr.error('系统错误');
		}
		
	});
	
	
	$('#s_province').change(function(){
		
		var parentId = $(this).val();
		
		$.ajax({
			url : ctx + '/manag/area/findArea', //市
			data : {type:3,parentId:parentId},
			dataType : 'json',
			type : 'post',
			success : function(json){
				
				var data = json.data
				
				if(json.status == "success"){
					
					var selected = $('#s_city').data('selected');
					
					$('#s_city').html('<option value="0">请选择</option>');
					
					for(var i=0; i<data.length; i++){
						if(selected != null &&　selected == data[i].id){
							$('#s_city').append('<option selected="selected" value="'+data[i].id+'">'+data[i].name+'</option>');
							$('#s_city').change();
						}else{
							$('#s_city').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
						}
					}
				}
			},
			error : function(){
				toastr.error('系统错误');
			}
			
		});
	})
	
	$('#s_city').change(function(){
		
		var parentId = $(this).val();
		
		$.ajax({
			url : ctx + '/manag/area/findArea', //区县
			data : {type:4,parentId:parentId},
			dataType : 'json',
			type : 'post',
			success : function(json){
				
				var data = json.data;
				
				if(json.status == "success"){
					
					var selected = $('#s_county').data('selected');
					
					$('#s_county').html('<option value="0">请选择</option>');
					
					for(var i=0; i<data.length; i++){
						if(selected != null &&　selected == data[i].id)
							$('#s_county').append('<option selected="selected" value="'+data[i].id+'">'+data[i].name+'</option>');
						else
							$('#s_county').append('<option value="'+data[i].id+'">'+data[i].name+'</option>');
					}
				}
			},
			error : function(){
				toastr.error('系统错误');
			}
			
		});
	})
	
	
	
	
	$("#organizationAddForm").bootstrapValidator();
	$("#organizationAddForm").on('success.form.bv', function(e) {
        e.preventDefault();
    });
	
	var from = $('#from').val();
	
	if(from == "check"){
		$('#organizationAddForm').find('input').attr('disabled', 'disabled');
		$('#organizationAddForm').find('select').attr('disabled', 'disabled');
		$('#organizationAddForm').find('textarea').attr('disabled', 'disabled');
		$('#organizationAddForm').find('button').attr('disabled', 'disabled');
	}

	$("body").on("focus.page", ".date-picker", function() {
		$(this).datetimepicker({
			format : "yyyy-mm-dd",
			minView : 2,
			autoclose : true,
			language : "zh-CN"
		})
	});

	$('#adds').click(function() {

		var addLesson = '<div class=" clear formleft" >'
				+ '<label>地<span>址</span></label>'
				+ ' <input type="text" class="formstyle">'
				+ '<i class="fa fa-minus sc" style="width:20px;height:20px;padding-left:10px;cursor:pointer"></i>'
		'</div>';
		$(".formbdcen").last().append(addLesson);
		$(".sc").click(function() {

			$(this).parent().remove();

		})
	})
	
	
	var departNum = 0;
	
	if(from == "check" || from == "edit"){
		departNum = $('#departNum').val();
	}
	$('.departAdd').click( function() {

		var addDepart = '<div class="form-group"><div class="col-sm-8"><input placeholder="请输入科室名称" name="depart['+departNum+'].name" type="text" class="form-control"/></div><div class="col-sm-1"><i class="fa fa-minus cunt_close family_i_1"></i></div></div>';
		
		$(".add_depart").last().append(addDepart).show("slow");
		
		departNum++;
		
	})
	
	$(document).on("click", ".cunt_close", function(){
		$(this).parent().parent().remove();
	});
	
});

/* 新增 */
$('#tagIdSubmitAddVip_btn').on('click', function() {
	setTimeout(function() {
		$('#closeCourse').click();
	}, 1000);
});

$("#city").click(function(e) {
	SelCity(this, e);
});


function validateOrganizationAddForm(form, callback, confirmMsg){
	
	 var $form = $(form);

/*     var formData = $form.data('bootstrapValidator');
     if (formData) {
    	 // 修复记忆的组件不验证
    	 formData.validate();

		if (!formData.isValid()) {
			return false;
		}
    }*/
     
    console.log($('#organizationAddForm').serialize());
   // return;
	
	$.ajax({
		url : ctx + '/medical/addMedical',
		data : $('#organizationAddForm').serialize(),
		dataType : 'json',
		type : 'post',
		success : function(data){
			if(data.status != "success"){
				toastr.warning(data.msg);
			}else{
				location.href = ctx + '/medical/organization';
			}
		},
		error : function(){
			toastr.error("系统错误");
		}
	});
	
	return false;
}




















